package stream;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class UseStreamDistinct {

    /**
     * 测试使用stream去重
     */
    @Test
    public void test() {
        List<Dish> dishList = new ArrayList<>();
        Dish dish1 = new Dish("001", "张三");
        dishList.add(dish1);
        Dish dish2 = new Dish("001", "李四");
        dishList.add(dish2);
        Dish dish3 = new Dish("002", "王五");
        dishList.add(dish3);
        Dish dish4 = new Dish("003", "阿森纳");
        dishList.add(dish4);
        show();

        /*
          测试Collectors.toCollection方法：将结果收集到其它类型的集合中(这里是TreeSet)
          TreeSet<Dish> treeSet2 = dishList.stream()
          .collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Dish::getId))));
        */

        List<Dish> collect = dishList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Dish::getId))), e -> new ArrayList(e)));
        System.out.println(collect);
    }

    public void show(){
        System.out.println("show");
    }


}
